package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class MySQLConnectionBean {
    // 更新为您的数据库连接信息
    private String jdbcDriver = "com.mysql.cj.jdbc.Driver"; // 使用新的驱动类名
    private String jdbcUrl = "jdbc:mysql://118.31.103.48:3066/vehicle?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8&allowPublicKeyRetrieval=true";
    private String user = "root";
    private String password = "123456";
    private boolean encrypt = false;
    private boolean trustServerCertificate = true;

    // 构造方法和原有逻辑保持不变
    public MySQLConnectionBean() {}

    public MySQLConnectionBean(String jdbcUrl, String user, String password, boolean encrypt, boolean trustServerCertificate) {
        this.jdbcUrl = jdbcUrl;
        this.user = user;
        this.password = password;
        this.encrypt = encrypt;
        this.trustServerCertificate = trustServerCertificate;
    }

    public Connection getConnection() throws SQLException {
        Properties connectionProps = new Properties();
        connectionProps.put("user", this.user);
        connectionProps.put("password", this.password);
        connectionProps.put("useSSL", "false");
        connectionProps.put("serverTimezone", "Asia/Shanghai");
        connectionProps.put("characterEncoding", "utf8");
        connectionProps.put("allowPublicKeyRetrieval", "true");

        try {
            // 加载JDBC驱动
            System.out.println("正在加载JDBC驱动: " + this.jdbcDriver);
            Class.forName(this.jdbcDriver);
            System.out.println("JDBC驱动加载成功");

            // 建立连接
            System.out.println("正在连接数据库: " + this.jdbcUrl);
            Connection conn = DriverManager.getConnection(this.jdbcUrl, connectionProps);
            System.out.println("数据库连接成功");

            return conn;
        } catch (ClassNotFoundException e) {
            System.err.println("JDBC驱动未找到: " + e.getMessage());
            throw new SQLException("JDBC driver not found", e);
        } catch (SQLException e) {
            System.err.println("数据库连接失败: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }

    public void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
                System.out.println("数据库连接已关闭");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // Getter and Setter 方法保持不变
    public String getJdbcDriver() { return jdbcDriver; }
    public void setJdbcDriver(String jdbcDriver) { this.jdbcDriver = jdbcDriver; }
    public String getJdbcUrl() { return jdbcUrl; }
    public void setJdbcUrl(String jdbcUrl) { this.jdbcUrl = jdbcUrl; }
    public String getUser() { return user; }
    public void setUser(String user) { this.user = user; }
    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }
    public boolean isEncrypt() { return encrypt; }
    public void setEncrypt(boolean encrypt) { this.encrypt = encrypt; }
    public boolean isTrustServerCertificate() { return trustServerCertificate; }
    public void setTrustServerCertificate(boolean trustServerCertificate) { this.trustServerCertificate = trustServerCertificate; }
}
